﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        moment: "components/moment/moment.min",
        datetime: "resources/js/datetime",
        dictionary: "resources/js/dictionary",
        common: "resources/js/common",
        wx: "http://res.wx.qq.com/open/js/jweixin-1.2.0",
        jssdk: "resources/js/jssdk"
    },
    shim: {
        "MINT": {
            deps: ["vue"],
            exports: "MINT"
        },
        "wx": {
            exports: "wx"
        }
    }
});

require(["zepto","dingtalk","vue", "axios", "common", "MINT", "moment", "datetime", "dictionary", "jssdk"], function (zepto,dingtalk,Vue, axios, common, MINT, moment, datetime, dictionary, jssdk) {

    Vue.component(MINT.Toast.name, MINT.Toast);

    window.MINT = MINT;

    var vm = new Vue({
        el: "#view",
        data: {
            title: "",
            data: {
                incidentType: "故障",
                title: "",
                description: "",
                happenTime: null,
                expectTime: null,
                occurrenceFrequency: ""
            },
            others: {
                linkmanName: "",
                userTel: "",
                userMobile: "",
                userEmail: ""
            },
            images: []
        },
        mounted: function () {
            var self = this;

            axios.get("/common/currentUser")
                .then(function (response) {
                    self.others.linkmanName = response.data.fullname;
                    self.others.userTel = response.data.tel;
                    self.others.userMobile = response.data.mobile;
                    self.others.userEmail = response.data.email;
                });
        },
        methods: {
            startVoice: function (event) {
                event.preventDefault();
                jssdk.startRecord(function (result) {
                    self.data.description = result;
                });
            },
            endVoice: function (event) {
                var self = this;
                event.preventDefault();
                jssdk.stopRecord(function (result) {
                    self.data.description = result;
                });
            },
            imgUpload: function (event) {
                common.imgUpload(event, this.images, MINT.Toast);
            },
            submit: function () {
                if (common.isEmpty(this.data.title)) {
                    MINT.Toast("请输入标题");
                    return;
                }
                if (common.isEmpty(this.data.description)) {
                    MINT.Toast("请输入事件详细描述");
                    return;
                }

                var data = {
                    data: this.data,
                    others: this.others,
                    images: this.images
                };

                data.others.longitude = jssdk.location().longitude.toString();
                data.others.latitude = jssdk.location().latitude.toString();

                axios.post("/incident/newIcd", data)
                    .then(function (response) {
                        alert("已报单");
                        common.goback();
                    });
            }
        }
    });
});